{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 39,
      "outputs": [],
      "source": "import numpy as np\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% \n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": "# numpy\n### numpy简介\n Python库，多维度数组的计算，提供大量的库函数，辅助进行矩阵计算。完成的计算任务主要包括\n* 机器学习模型：\n* * 编写机器学习算法的时候，需要对矩阵进行大量的计算。包括矩阵乘法，换位，加法等。*\n* * Numpy数组，用于存储训练数据，存储机器学习模型参数*\n* 图像处理和计算机图形学：\n* * 图像表示为多维度的数字数组，numpy提供了优秀的库函数处理图像，如镜像图像，旋转\n* 数学任务：\n* * 应度数学计算，数值积分，微分，内插，外推，基本可替代matlab*\n  "
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": "### numpy ndarray n维数组对象\nNumPy提供的最重要的数据结构是一个称为NumPy数组的强大的n维数组对象。\nNumPy数组配备了大量的函数和运算符\n* ndarray 对象是用于存放同类型元素的多维数组。\n* ndarray 中的每个元素在内存中都有相同存储大小的区域。\n\n* ndarry的组成\n* * 一个指向数据（内存或内存映射文件中的一块数据）的指针。\n* * 数据类型或 dtype，描述在数组中的固定大小值的格子。\n* * 一个表示数组形状（shape）的元组，表示各维度大小的元组。\n* * 一个跨度元组（stride），其中的整数指的是为了前进到当前维度下一个元素需要\"跨过\"的字节数。\n\n\n### np.array() 对象\n* numpy.array(object, dtype \u003d None, copy \u003d True, order \u003d None, subok \u003d False, ndmin \u003d 0)\n* * object\t数组或嵌套的数列\n* * dtype\t数组元素的数据类型，可选 自定义元素种类不同的时候可以使用 object 参数\n* * copy\t对象是否需要复制，可选\n* * order\t创建数组的样式，C为行方向，F为列方向，A为任意方向（默认）\n* * subok\t默认返回一个与基类类型一致的数组\n* *ndmin\t指定生成数组的最小维度\n"
    },
    {
      "cell_type": "markdown",
      "source": "### numpy矩阵支持的数据类型\n*    bool_\t布尔型数据类型（True 或者 False）\n*    int_\t默认的整数类型（类似于 C 语言中的 long，int32 或 int64）\n*    intc\t与 C 的 int 类型一样，一般是 int32 或 int 64\n*    int8\t字节（-128 to 127）\n*    int16\t整数（-32768 to 32767）\n*    int32\t整数（-2147483648 to 2147483647）\n*    int64\t整数（-9223372036854775808 to 9223372036854775807）\n*    uint8\t无符号整数（0 to 255）\n*    uint16\t无符号整数（0 to 65535）\n*    uint32\t无符号整数（0 to 4294967295）\n*    uint64\t无符号整数（0 to 18446744073709551615）\n*    float_\tfloat64 类型的简写\n*    float16\t半精度浮点数，包括：1 个符号位，5 个指数位，10 个尾数位\n*    float32\t单精度浮点数，包括：1 个符号位，8 个指数位，23 个尾数位\n*    float64\t双精度浮点数，包括：1 个符号位，11 个指数位，52 个尾数位\n*    complex_\tcomplex128 类型的简写，即 128 位复数\n*    complex64\t复数，表示双 32 位浮点数（实数部分和虚数部分）\n*    complex128\t复数，表示双 64 位浮点数（实数部分和虚数部分）\n*    python中通用的类型是object对象\n*    同一个矩阵中的元素类型须相同\n*    object 矩阵元素可以不同类型\n*    当复杂的数据类型中,数组需要同时支持字符串,整形,以及数组\n*    可通过将.dtype 设定为 dpyte \u003d object 对象即可  \n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### np.ndarry 属性\n* 1 .shape 查看数组维度 行列数\n* 2 .dtype 查看数组类型 且numpy的数组中的数据类型必须是一致的 同时作为数组的类型的定义参数\n* 3 .itemsize 查看元素字节\n* 4 .size 查看数组中的元素个数\n* 5 .ndim 查看数组维度信息\n* 6 .flags 查看数组对象内存信息",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "import numpy as np\nmy_array \u003d np.array([(1,2,3),(4,5,6)], dtype\u003dstr)\nprint(my_array.shape)  # 查看数组的维度\nprint(my_array.dtype)  # 查看数据类型\nprint(my_array.itemsize) # 查看数组中的元素的字节大小\nprint(my_array.size)  # 查看数组中有多少个元素  可以直接作用于列表\nprint(my_array.nbytes) # 查看数组总体的占字节量\nprint(my_array.ndim)  # 查看数据维度\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 数组的创建 数据的导入\n* 1 通过np.genfromtxt(路径, 分隔符, 数据类型) 导入矩阵文件 \n* 2 通过numpy.asarray(a, dtype \u003d None, order \u003d None) 生成构建数组数据结构  不限维度\n* 3 通过np.zeros 生成全0数组\n* 4 通过np,ones 全1 数组\n* 5 通过np.arange(start, stop, step, dtype) 指定范围的数组 支持浮点数  可指定开头,结尾,步长变化\n* 6 通过np.linspace 指定生成指定范围的等线性数组 默认数组元素个数是50\n* 7 通过np.asarray 从已有的数组创建数组\n* 8 通过np.asarray 创建\n* 9 通过np.array(dtye\u003d object) 创建多种元素的数组对象",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 1 np.genfromtxt(path, delimiter, dtype)导入数据\n* * path 文件路径\n* * delimiter 分隔符\n* * dtype 导入的数据类型",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 41,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "[[\u0027Year\u0027 \u0027WHO region\u0027 \u0027Country\u0027 \u0027Beverage Types\u0027 \u0027Display Value\u0027]\n [\u00271986\u0027 \u0027Western Pacific\u0027 \u0027Viet Nam\u0027 \u0027Wine\u0027 \u00270\u0027]\n [\u00271986\u0027 \u0027Americas\u0027 \u0027Uruguay\u0027 \u0027Other\u0027 \u00270.5\u0027]\n ...\n [\u00271987\u0027 \u0027Africa\u0027 \u0027Malawi\u0027 \u0027Other\u0027 \u00270.75\u0027]\n [\u00271989\u0027 \u0027Americas\u0027 \u0027Bahamas\u0027 \u0027Wine\u0027 \u00271.5\u0027]\n [\u00271985\u0027 \u0027Africa\u0027 \u0027Malawi\u0027 \u0027Spirits\u0027 \u00270.31\u0027]]\n\u003cclass \u0027numpy.ndarray\u0027\u003e\n\u003cU52\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "import numpy as np\nworld_alcohol \u003d np.genfromtxt(\"world_alcohol.txt\", delimiter\u003d\",\", dtype\u003dstr)\nprint(world_alcohol)\nprint(type(world_alcohol))  # 查看导入的数据类型\nprint(world_alcohol.dtype)\n# print(help(np.genfromtxt)) # 查看该函数的帮助\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 2 numpy.arange(start, stop, step, dtype) 生成指定数组 \n* 一维列表 二维列表的列表 \n* 三维列表的列表的列表",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "vector1 \u003d np.array([1,2,3,4])  # 一维数组 列表\nvector2  \u003d np.array([[1,2,3],[4,5,6],[7,8,9]])  # 二维数组,列表的列表\nvector3 \u003d np.array([[[1,2,3],[4,5,6]]])   # 三维数组列表的列表的列表\nprint(vector1.ndim)  # 查看维数",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 3 np.zeros 生成指定维度全0矩阵",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "aa \u003d np.zeros((2,3,3))  # 生成指定维度的0数组\nprint(aa)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 4 np.ones 生成指定维度的全1数组",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 42,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "[[[1. 1. 1. 1.]\n  [1. 1. 1. 1.]]\n\n [[1. 1. 1. 1.]\n  [1. 1. 1. 1.]]]\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "bb \u003d np.ones((2,2,4))  # 生成指定维度的全1数组\nprint(bb)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 5 生成指定步长变化的数组",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "cc \u003d np.arange(1,10,2)  # 生成指定步长的一位数组 不包括结尾\nprint(cc)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 6 np.linspace(起始值, 终点值, 值个数) 生成指定个数的线性空间数组 默认50个",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "dd \u003d np.linspace(1.,12.,7) # 生成指定个数的一维数组\nprint(dd)\nprint(np.linspace(1, 20, 100))",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "\n* 7 np.random,rand(行, 列) 生成指定维度(行列) 元素值为(-1, +1) 范围的随机矩阵",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 权重初始化的时候生成random矩阵\n# np.random,rand(行, 列) # 生成-1 - +1 范围的指定行列的矩阵\nvector10 \u003d np.random.rand(2,3,3)  # random模块中的rand方法的使用生成随机值 默认-1至+1范围\nvector12 \u003d np.random.rand(2,3,3)  # 扩大倍数\nprint(vector10)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 8 np.asarray 创建",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "import numpy as np\nb \u003d np.array([1,2,3])\nprint(b)\nprint(b.dtype)\nc \u003d np.array(b, dtype\u003d\"float\") #会产生一个新的数组\nd \u003d np.asarray(b, dtype\u003d\"float\") # 不会生成一个新的数组\nb.astype(float)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 9 自定义object 类型的数组对象",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "import numpy as np\na \u003d np.array([1,2,3,\"123\",\"abc\",[1,2]], dtype\u003dobject)\nprint(a)\nprint(type(a))  # 查看a的类型\nprint(a.dtype)  # 查看a的数组类型\nprint(a.shape)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 矩阵数据处理\n* 1 通过np.ravel 矩阵转换为一维向量\n* 2 通过np.reshape 矩阵转换为指定行列(维度)的向量\n* 3 通过np.hstack 矩阵的纵向扩展 添加某列的记录值\n* 4 通过np.vstack 矩阵的横向扩展 添加记录的条数",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 1 矩阵转换为一维向量  .ravel()",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "print(vector13.ravel())  # 矩阵转换为一维向量(拉平)\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 2 np.reshape 对矩阵重新shape (改变行列数)\n*   矩阵reshape   (n, -1)  当第二个参数为-1 的时候, 自动根据第一个参数自动计算(大矩阵使用)",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "print(vector13.ravel().reshape(3,2)) # 拉平后在重新shape\nprint(vector13.ravel().reshape(3, -1)) # 拉平后在重新shape 自动计算\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 3 np.hstack(v1, v2) 矩阵纵向扩展 记录的属性值添加",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "[[1 2 3 7 8 9]\n [4 5 6 1 4 7]]\n[[1 2 3]\n [4 5 6]\n [7 8 9]\n [1 4 7]]\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "vector15 \u003d np.array([[1,2,3],[4,5,6]])\nvector16 \u003d np.array([[7,8,9],[1,4,7]])\nprint(np.hstack((vector15, vector16)))\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "* 4 np.vstack(v1,v2) 矩阵记录叠加增加样本",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "vector15 \u003d np.array([[1,2,3],[4,5,6]])\nvector16 \u003d np.array([[7,8,9],[1,4,7]])\nprint(np.vstack((vector15, vector16)))\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": "### 数组的索引切片 \n* 0 索引:查找  切片:修改\n* 1 一维数组索引,切片\n* 2 多维度数组索引,切片\n* 3 矩阵的行记录切片, 列记录属性切片\n* 4 索引空记录并删除\n* 注:索引包含前面起始位置,但是不包含数组的索引的结束位置"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": "* 1 一维数组索引切片"
    },
    {
      "cell_type": "code",
      "execution_count": 43,
      "metadata": {
        "pycharm": {
          "is_executing": false
        }
      },
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "3\n[2 4]\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "ee \u003d np.array([1,2,3,4])\nprint(ee[2]) # 索引得到数组元素值\ncc \u003d ee[1::2]  # 切片得到新的数组\nprint(cc) # 切片得到数组的一部分,仍然是数组\n"
    },
    {
      "cell_type": "markdown",
      "source": "* 2 多维度数组的shape的变换 索引,切片",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": 35,
      "outputs": [
        {
          "name": "stdout",
          "text": [
            "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]\n[[ 0  1  2  3  4]\n [ 5  6  7  8  9]\n [10 11 12 13 14]\n [15 16 17 18 19]]\n0\n1\n[ 1  6 11 16]\n[[ 0  1  2  3  4]\n [ 5  6  7  8  9]\n [10 11 12 13 14]\n [15 16 17 18 19]]\n[[ 6  7  8]\n [11 12 13]]\n"
          ],
          "output_type": "stream"
        }
      ],
      "source": "ff \u003d np.arange(20)\nprint(ff) # 生成一维数组\nff.shape\u003d(4,5) # 对数组进行shape的切换 \nprint(ff)\nprint(ff[0][0]) # 二维数组的索引\nprint(ff[0,1])  # 二维的数组缩影方式2 索引0行1列的值\nprint(ff[:,1])  # 取所有行的第一列元素\nprint(ff[:,:])  # 取数组的所有行列元素  \nprint(ff[1:3,1:4])  # 取 1-2 行的 1-3列 注意元素索引要点 包头不包尾\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n",
          "is_executing": false
        }
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": ""
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 矩阵属性值均匀切分 np.hsplit(v1, 切的份数) 例如 2x12 切成 2x4 的3份\nvector17 \u003d np.floor(10* np.random.rand(2, 12))\nprint(\"2行12矩阵\\n\", vector17)\nprint(\"属性切分\\n\", np.hsplit(vector17, 3))\n\n# 矩阵的属性的指定位置的切分 np.hsplit(v, (列1后切, 列2后切...)\nprint(\"属性指定位置切分\", np.hsplit(vector17, (3, 4, 5)))\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% 矩阵属性的切分\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 按照行进行均匀切分 np.vsplit(v, 行均切份数)\nvector18 \u003d np.floor(10* np.random.rand(12, 2))\nprint(vector18)\nprint(np.vsplit(vector18, 3)) # 12行记录, 切成每个矩阵3个记录\n\n# 按照行进行定制切分 np.vsplit(v, (行1, 行2...))\nprint(\"指定行切\\n\",np.vsplit(vector18, (2, 3, 6))) # 从1开始数\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% 矩阵记录的切分\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": "### 数组的迭代查找 通过for循环输出行 或者 np.ndenumerate枚举"
    },
    {
      "cell_type": "code",
      "execution_count": 99,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[0 1 2 3 4]\n",
            "\u003cclass \u0027numpy.ndarray\u0027\u003e\n",
            "[5 6 7 8 9]\n",
            "\u003cclass \u0027numpy.ndarray\u0027\u003e\n",
            "[10 11 12 13 14]\n",
            "\u003cclass \u0027numpy.ndarray\u0027\u003e\n",
            "[15 16 17 18 19]\n",
            "\u003cclass \u0027numpy.ndarray\u0027\u003e\n"
          ]
        }
      ],
      "source": [
        "for i in ff:\n",
        "    print(i)\n",
        "    print(type(i))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 101,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "(0, 0) 0\n",
            "(0, 1) 1\n",
            "(0, 2) 2\n",
            "(0, 3) 3\n",
            "(0, 4) 4\n",
            "(1, 0) 5\n",
            "(1, 1) 6\n",
            "(1, 2) 7\n",
            "(1, 3) 8\n",
            "(1, 4) 9\n",
            "(2, 0) 10\n",
            "(2, 1) 11\n",
            "(2, 2) 12\n",
            "(2, 3) 13\n",
            "(2, 4) 14\n",
            "(3, 0) 15\n",
            "(3, 1) 16\n",
            "(3, 2) 17\n",
            "(3, 3) 18\n",
            "(3, 4) 19\n"
          ]
        }
      ],
      "source": [
        "for index, v in np.ndenumerate(ff):  # 通过np.ndenumerate枚举获取元素的索引位置以及元素值\n",
        "    print(index, v)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": "### 矩阵的计算\n* 数组求和 .sum\n* 数组积\n* 内积\n* 外积\n* 向量积\n* 点积\n* 矩阵最大值\n* 矩阵最小值"
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "pycharm": {}
      },
      "source": "#### 数组元素求和"
    },
    {
      "cell_type": "code",
      "execution_count": 107,
      "metadata": {
        "pycharm": {}
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[ 0  1  2  3  4]\n",
            " [ 5  6  7  8  9]\n",
            " [10 11 12 13 14]\n",
            " [15 16 17 18 19]]\n",
            "[30 34 38 42 46]\n",
            "[10 35 60 85]\n",
            "\u003cbuilt-in method sum of numpy.ndarray object at 0x000001BC136CC0D0\u003e\n"
          ]
        }
      ],
      "source": "aaa \u003d np.arange(20)\naaa.shape\u003d(4,5)\nprint(aaa)\nprint(aaa.sum(axis\u003d0))  # 求矩阵的列和\nprint(aaa.sum(axis\u003d1))  # 求矩阵的行和\nprint(aaa.sum)\n"
    },
    {
      "cell_type": "markdown",
      "source": "### bool矩阵逻辑判断 数据清洗\n* ",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% md\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 逻辑判断 查找元素或者记录\n# 判断数组中的某个值是否为某个具体值\nworld_alcohol2 \u003d np.array([[1,2,3],[1,4,3]])\n#print(world_alcohol2)\n#print(world_alcohol2 \u003d\u003d 3)  # 返回布尔判断\n\n# 以布尔判断为索引取出相应的元素值\nequl_to_two \u003d world_alcohol2 \u003d\u003d 2\nequl_to_2 \u003d (world_alcohol2[:,1] \u003d\u003d 2) # 找出属性1(第2列)为2的布尔\nprint(equl_to_2)\nprint(world_alcohol2[equl_to_2,:])  # 找出所有记录中的属性1 即第2列为2的记录\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "print(vector6.min())  # 求极值\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% 对数据求极值 .min\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "vector8 \u003d np.array([[1,2,3],[4,5,6],[7,8,9]])\nprint(\"返回行和矩阵\", vector8.sum(axis\u003d1))\nprint(\"返回列和矩阵\", vector8.sum(axis\u003d0))\n\n\n\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% 求和 对某行某个记录或者某列求和, 指定维度 .sum(axis\u003d1)行求和  .sum(axis\u003d0)列求和\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 乘法 矩阵的元素对应相乘 得到矩阵 方阵相乘\n# 内积 np.dot(vector1, vector2) 得到新的矩阵源矩阵的行,列\n\nvector11 \u003d np.arange(6).reshape(2,3)\nvector12 \u003d np.arange(6).reshape(3,2)\nprint(vector11)\nprint(vector12)\nprint(\"内积\",np.dot(vector11,vector12))\n\n### 数组加1",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% 矩阵乘法\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "print(a+1) # 数组中的所有元素加1",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "### 数组乘法",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "print(a*a) # 数组对应的元素相乘",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%%\n"
        }
      }
    },
    {
      "cell_type": "markdown",
      "source": "",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "# 倍化随机矩阵 random的范围是-1 - +1 使用倍乘是元素增加\nvector13 \u003d 10* np.floor(10* np.random.rand(2 ,3)) # floor 向下取整\nprint(vector13)\n\n\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% 矩阵相关操作\n"
        }
      }
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "outputs": [],
      "source": "#   \u003d 深复制, 引用复制, 复制以后绑定的在内存中同一个地址,\n#  只是名字不一样, a变, b也变\nvector19 \u003d np.arange(12).reshape(3,4)\ncopy_vector19 \u003d vector19\nprint(copy_vector19 is vector19)  # 使用 \u003d 进行复制 二者同样只是名字不一样\n\n# 浅复制 v.view()\ncopy_light_vector19 \u003d vector19.view()\nprint(copy_light_vector19 is vector19) # 浅复制后的矩阵并不是原矩阵\ncopy_light_vector19.shape \u003d 2,6  # 对浅复制进行reshape\nprint(copy_light_vector19.shape, vector19.shape)  # 可以看出shape改变互不影响\n# 浅复制,虽然变量名所指的位置不一样,但是实际上,浅复制仍然共用矩阵中的值\n# 即,改变a矩阵中的值, 浅复制的矩阵的值也会改变\n\n\n",
      "metadata": {
        "pycharm": {
          "metadata": false,
          "name": "#%% 矩阵的复制\n"
        }
      }
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.5"
    },
    "stem_cell": {
      "cell_type": "raw",
      "source": "",
      "metadata": {
        "pycharm": {
          "metadata": false
        }
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}